﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Edit : System.Web.UI.Page
{
    String strCon;
    String strCom;
    SqlConnection objCon;
    SqlCommand objCom;
    SqlDataReader dtReader;
    SqlDataAdapter dtAdapter;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBindData();
            BindData(Convert.ToInt32(Request.QueryString["id"]));
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "UPDATE NontananYe_SQLLogin_1.Customer SET Cus_FirstName = '" + txtCustomerFirstName.Text
            + "', Cus_LastName = '" + txtCustomerLastName.Text
            + "', Cus_NickName = '" + txtCustomerNickName.Text
            + "', Cus_FavoritesFood = " + rblFood.SelectedValue
            + " OUTPUT INSERTED.Cus_ID"
            + " WHERE Cus_ID = " + Request.QueryString["id"];
        objCom = new SqlCommand(strCom, objCon);
        try
        {
            int newCusID = 0;
            newCusID = (int)objCom.ExecuteScalar();

            strCom = "DELETE FROM NontananYe_SQLLogin_1.Customer_Detail WHERE CusDetail_Cus_ID =" + newCusID;
            objCom = new SqlCommand(strCom, objCon);
            objCom.ExecuteNonQuery();

            foreach (ListItem item in cblColor.Items)
            {
                if (item.Selected)
                {
                    strCom = "INSERT INTO NontananYe_SQLLogin_1.Customer_Detail (CusDetail_Cus_ID, CusDetail_Color_ID) " +
                    " VALUES "
                    + " ('" + newCusID
                    + "','" + item.Value + "');";
                    objCom = new SqlCommand(strCom, objCon);
                    objCom.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Server.Transfer("Select.aspx");
    }

    private void BindData(int id)
    {
        strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "Select Cus_ID,Cus_FirstName,Cus_LastName,Cus_NickName,Cus_FavoritesFood From NontananYe_SQLLogin_1.Customer WHERE Cus_ID='" + id + "'";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();

        while (dtReader.Read())
        {
            txtCustomerFirstName.Text = dtReader[1].ToString();
            txtCustomerLastName.Text = dtReader[2].ToString();
            txtCustomerNickName.Text = dtReader[3].ToString();
            rblFood.SelectedValue = dtReader[4].ToString();
        }

        dtReader.Close();
        dtReader = null;

        CheckedListBindData(id);
    }
    private void ListBindData()
    {
        strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        objCon = new SqlConnection(strCon);
        objCon.Open();
        //rbl
        strCom = "Select F_ID,F_Name From NontananYe_SQLLogin_1.Food";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();
        rblFood.DataSource = dtReader;
        rblFood.DataValueField = "F_ID";
        rblFood.DataTextField = "F_Name";
        rblFood.DataBind();

        dtReader.Close();
        dtReader = null;
        //cbl
        strCom = "Select C_ID,C_Name From NontananYe_SQLLogin_1.Color";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();
        cblColor.DataSource = dtReader;
        cblColor.DataValueField = "C_ID";
        cblColor.DataTextField = "C_Name";
        cblColor.DataBind();

        dtReader.Close();
        dtReader = null;
    }
    private void CheckedListBindData(int id)
    {
        strCom = "Select CusDetail_Color_ID From NontananYe_SQLLogin_1.Customer_Detail WHERE CusDetail_Cus_ID='" + id + "'";
        objCom = new SqlCommand(strCom, objCon);
        dtAdapter = new SqlDataAdapter(objCom);

        dtAdapter.Fill(dt);

        for (int i = 0; i < dt.Rows.Count; i++)
            cblColor.Items[Convert.ToInt32(dt.Rows[i][0]) - 1].Selected = true;

        dtAdapter = null;
    }

}